<?php

namespace app\common\service\Oss;

class Bucket extends Common
{
    /**
     * 创建一个存储空间
     * acl 指的是bucket的访问控制权限，有三种，私有读写，公共读私有写，公共读写。
     * 私有读写就是只有bucket的拥有者或授权用户才有权限操作
     * 三种权限分别对应 (OssClient::OSS_ACL_TYPE_PRIVATE，OssClient::OSS_ACL_TYPE_PUBLIC_READ, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE)
     *
     * @param OssClient $this->_ossClient OssClient实例
     * @param string $bucket 要创建的存储空间名称
     * @return null
     */
    public function createBucket($bucket)
    {
        try {
            $this->_ossClient->createBucket($bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    /**
     *  判断Bucket是否存在
     *
     * @param OssClient $this->_ossClient OssClient实例
     * @param string $bucket 存储空间名称
     */
    public function doesBucketExist($bucket)
    {
        try {
            $res = $this->_ossClient->doesBucketExist($bucket);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        if ($res === true) {
            print(__FUNCTION__ . ": OK" . "\n");
        } else {
            print(__FUNCTION__ . ": FAILED" . "\n");
        }
    }

    /**
     * 删除bucket，如果bucket不为空则bucket无法删除成功， 不为空表示bucket既没有object，也没有未完成的multipart上传时的parts
     *
     * @param OssClient $this->_ossClient OssClient实例
     * @param string $bucket 待删除的存储空间名称
     * @return null
     */
    public function deleteBucket($bucket)
    {
        try {
            $this->_ossClient->deleteBucket($bucket);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    /**
     * 设置bucket的acl配置
     *
     * @param OssClient $this->_ossClient OssClient实例
     * @param string $bucket 存储空间名称
     * @return null
     */
    public function putBucketAcl($bucket)
    {
        $acl = OssClient::OSS_ACL_TYPE_PRIVATE;
        try {
            $this->_ossClient->putBucketAcl($bucket, $acl);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }


    /**
     * 获取bucket的acl配置
     *
     * @param OssClient $this->_ossClient OssClient实例
     * @param string $bucket 存储空间名称
     * @return null
     */
    public function getBucketAcl($bucket)
    {
        try {
            $res = $this->_ossClient->getBucketAcl($bucket);
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
        print('acl: ' . $res);
    }


    /**
     * 列出用户所有的Bucket
     *
     * @param OssClient $this->_ossClient OssClient实例
     * @return null
     */
    public function listBuckets()
    {
        try {
            $bucketListInfo = $this->_ossClient->listBuckets();
        } catch (OssException $e) {
            throw_exception($e->getMessage());
            return false;
        }
        $bucketList = $bucketListInfo->getBucketList();
        foreach ($bucketList as$bucket) {
            $data[] = ["location" => $bucket->getLocation(),
                        "name" => $bucket->getName(),
                        "add_date" =>$bucket->getCreatedate()];
        }
        return $data;
    }
}
